#pragma once

#include <QElapsedTimer>
#include <QOpenGLFunctions>
#include <QOpenGLWidget>

class QOpenGLBuffer;
class QOpenGLFunctions;
class QOpenGLTexture;
class QOpenGLVertexArrayObject;
class QOpenGLShaderProgram;

class QPictureBox : public QOpenGLWidget, private QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit QPictureBox(QWidget *parent = 0);
    enum ScaleType {
        fitXY,
        fitCenter,
        CenterCrop,
    };
    Q_ENUM(ScaleType)

    enum FilterType {
        Nearest = 0x2600,
        Linear = 0x2601,
    };
    Q_ENUM(FilterType)

    void setImage(const QImage &image);
    void setScaleType(enum ScaleType scaleType);
    void setBackGround(QColor color);
    void setFilterType(enum FilterType filterType);
    uint32_t fps();

Q_SIGNALS:
    void fpsChanged(int fps);

private:
    QOpenGLShaderProgram *m_program = nullptr;
    QOpenGLBuffer *m_vbo = nullptr;
    QOpenGLVertexArrayObject *m_vao = nullptr;
    QOpenGLTexture *m_texture = nullptr;
    QImage m_image;
    QColor m_background = QColor(0, 0, 0, 255);
    enum ScaleType m_scaleType = ScaleType::fitCenter;
    enum FilterType m_filterType = FilterType::Nearest;
    uint32_t m_frameCount = 0;
    uint32_t m_currentFps = 0;
    QElapsedTimer m_fpsTimer;

    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;
    void resetTexture();
};

Q_DECLARE_METATYPE(QPictureBox::ScaleType)
Q_DECLARE_METATYPE(QPictureBox::FilterType)
